package com.ghh.dao;

import com.ghh.pojo.Role;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

public interface RoleDao {

    @Select("SELECT r.* FROM t_role r,t_user_role ur where r.id = ur.role_id and ur.user_id = #{userId}")
    Set<Role> findByUserId(Integer userId);

    @Select("select ur.role_id from t_user u INNER JOIN t_user_role ur ON u.id = ur.user_id WHERE u.username=#{username}")
    List<Integer> findByUsernmae(String username);

    //分页查询
    @Select("<script>select * from t_role <if test=\"value!=null and value!=''\">where name=#{value}</if></script>")
    Page<Role> findPage(@Param("value") String queryString);

    //添加并返回id
    @Insert("insert into t_role values (null,#{name},#{keyword},#{description})")
    @SelectKey(statement = "select last_insert_id()",before = false,keyProperty = "id",resultType = Integer.class,keyColumn = "id")
    void add(Role role);

    //添加role和permission中间表
    @Insert("insert into t_role_permission (#{role_id},#{permission_id})")
    void setPermissionAndRole(Map<String, Integer> map);

    //添加role和menu中间表
    @Insert("insert into t_role_menu (#{role_id},#{menu_id})")
    void setMenuAndRole(Map<String, Integer> map2);

    //根据ID查询角色信息
    @Select("select * from t_role where id = #{id}")
    Role findById(Integer id);

    //根据角色id查询关联的权限Id
    @Select("select permission_id from t_role_permission where role_id = #{id}")
    List<Integer> findPermissionIdsByRoleId(Integer id);

    //根据角色id查询关联的菜单Id
    @Select("select menu_id from t_role_menu where role_id = #{id}")
    List<Integer> findMenuIdsByRoleId(Integer id);

    //修改基本信息
    @Update("update t_role set name = #{name},keyword = #{keyword},description = #{description} where id = #{id}")
    void edit(Role role);

    //根据角色ID删除关联表role-permission数据
    @Delete("delete from t_role_permission where role_id = #{id}")
    void deleteRoleAndPermissionByRoleId(Integer id);

    //根据角色ID删除关联表role-menu数据
    @Delete("delete from t_role_menu where role_id = #{id}")
    void deleteRoleAndMenuByRoleId(Integer id);

    //根据角色id删除角色
    @Delete("delete from t_role where id = #{id}")
    void deleteByRoleId(Integer id);
}
